package com.lagou.config;

import com.lagou.pojo.Configuration;
import com.lagou.pojo.MappedStatement;
import org.apache.ibatis.binding.BindingException;
import org.apache.ibatis.mapping.SqlCommandType;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Properties;

public class XMLMapperBuilder {

    private Configuration configuration;


    public XMLMapperBuilder(Configuration configuration) {
        this.configuration =configuration;
    }




    public void parse(InputStream inputStream, Properties variables) throws DocumentException {

        Document document = new SAXReader().read(inputStream);
        Element rootElement = document.getRootElement();

        String namespace = rootElement.attributeValue("namespace");
//        MappedStatement mappedStatement = new MappedStatement();
//        SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType();
//        //判断mapper中的方法类型，最终调用的还是SqlSession中的方法
//        List<Element> list;
//        switch (sqlCommandType) {
//            case INSERT: {
//                list = rootElement.selectNodes("//insert");
//                break;
//            }
//            case UPDATE: {
//                list = rootElement.selectNodes("//update");
//                break;
//            }
//            case DELETE: {
//                list = rootElement.selectNodes("//delete");
//                break;
//            }
//            case SELECT:
//                list = rootElement.selectNodes("//select");
//                break;
//            default:
//                throw new BindingException("Unknown execution method for: " + sqlCommandType);
//        }
        List<Element> list = new ArrayList<>();
        if (rootElement.selectNodes("//select")!=null){
            list.addAll(rootElement.selectNodes("//select"));
        }
        if (rootElement.selectNodes("//insert")!=null){
            list.addAll(rootElement.selectNodes("//insert"));
        }
        if (rootElement.selectNodes("//update")!=null){
            list.addAll(rootElement.selectNodes("//update"));
        }
        if (rootElement.selectNodes("//delete")!=null){
            list.addAll(rootElement.selectNodes("//delete"));
        }

        for (Element element : list) {
            String id = element.attributeValue("id");
            String resultType = element.attributeValue("resultType");
            String paramterType = element.attributeValue("paramterType");
            String sqlText = element.getTextTrim();
            MappedStatement mappedStatement = new MappedStatement();
            mappedStatement.setId(id);
            mappedStatement.setResultType(resultType);
            mappedStatement.setParamterType(paramterType);
            mappedStatement.setSql(sqlText);
            String key = namespace+"."+id;
            configuration.getMappedStatementMap().put(key,mappedStatement);

        }

    }


}
